package com.c2b.algorithm.leetcode.base;

/**
 * <a href='https://leetcode.cn/problems/sort-list/'>排序链表(Sort List)</a>
 * <p>给你链表的头结点 head ，请将其按 升序 排列并返回 排序后的链表 。</p>
 *
 * <p>
 * <b>示例：</b>
 * <pre>
 * 示例 1：
 *      输入：head = [4,2,1,3]
 *      输出：[1,2,3,4]
 *
 * 示例 2：
 *      输入：head = [-1,5,3,4,0]
 *      输出：[-1,0,3,4,5]
 *
 * 示例 3：
 *      输入：head = []
 *      输出：[]
 * </pre>
 * </p>
 *
 * <p>
 * <b>提示：</b>
 *     <ul>
 *         <li>链表中节点的数目在范围 [0, 5 * 10^4] 内</li>
 *         <li>-10^5 <= Node.val <= 10^5</li>
 *     </ul>
 * </p>
 * <b>进阶：你可以在 O(n log n) 时间复杂度和常数级空间复杂度下，对链表进行排序吗？</b>
 *
 * @author c2b
 * @see LC0002AddTwoNumbers_M 两数相加(Add Two Numbers)
 * @see LC0019RemoveNthNodeFromEndOfList_M 删除链表的倒数第 N 个结点(Remove Nth Node From End of List)
 * @see LC0021MergeTwoSortedLists_S 合并两个有序链表(Merge Two Sorted Lists)
 * @see LC0023MergeKSortedLists 合并 K 个升序链表(Merge k Sorted Lists)
 * @see LC0024SwapNodesInPairs_M 两两交换链表中的节点(Swap Nodes in Pairs)
 * @see LC0025ReverseNodesInKGroup_H K 个一组翻转链表(Reverse Nodes in k-Group)
 * @see LC0138CopyListWithRandomPointer_M 随机链表的复制(Copy List with Random Pointer)
 * @see LC0141LinkedListCycle_S 环形链表(Linked List Cycle)
 * @see LC0142LinkedListCycle_II_M 环形链表 II(Linked List Cycle II)
 * @see LC0146LRUCache_M LRU 缓存(LRU Cache)
 * @see LC0148SortList_M 排序链表(Sort List)
 * @see LC0160IntersectionOfTwoLinkedLists_S 相交链表(Intersection of Two Linked Lists)
 * @see LC0206ReverseLinkedList_S 反转链表(Reverse Linked List)
 * @see LC0234PalindromeLinkedList_S 回文链表(Palindrome Linked List)
 * @since 2023/11/1 11:17
 */
public class LC0148SortList_M {
    static class Solution {
        /**
         * 排序链表
         * // 1.排序前半段[firstHalfHead,firstHalfTail]，
         * // 2.排序后半段[secondHalfHead,secondHalfTail]
         * // 3.合并排序好的链表
         */
        public ListNode sortList(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            // 找到中间节点
            ListNode slow = head;
            ListNode fast = head;
            while (fast.next != null && fast.next.next != null) {
                slow = slow.next;
                fast = fast.next.next;
            }
            // 链表分成两段
            ListNode second = slow.next;
            slow.next = null;
            // 分别对两段链表进行排序，并将排序后的链表进行合并
            return merge(sortList(head), sortList(second));
        }

        private ListNode merge(ListNode list1, ListNode list2) {
            ListNode dummyHead = new ListNode(-1);
            ListNode tempNode = dummyHead;
            while (list1 != null && list2 != null) {
                if (list1.val <= list2.val) {
                    tempNode.next = list1;
                    list1 = list1.next;
                } else {
                    tempNode.next = list2;
                    list2 = list2.next;
                }
                tempNode = tempNode.next;
            }
            tempNode.next = list1 != null ? list1 : list2;
            return dummyHead.next;
        }
    }


    public static void main(String[] args) {
        ListNode node = new ListNode(4);
        node.next = new ListNode(3);
        //node.next.next = new ListNode(2);
        //node.next.next.next = new ListNode(1);
        //node.next.next.next.next = new ListNode(5);
        Solution solution = new Solution();
        Printer.printListNode(solution.sortList(node));
    }
}
